package com.aviccxzx.smarthome.UserModule;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import com.aviccxzx.smarthome.ConfigModule.GosAirlinkChooseDeviceWorkWiFiActivity;
import com.aviccxzx.smarthome.ConfigModule.GosConfigModuleBaseActivity;
import com.aviccxzx.smarthome.ConfigModule.GosDeviceReadyActivity;
import com.aviccxzx.smarthome.DeviceModule.GosDeviceModuleBaseActivity;
import com.aviccxzx.smarthome.R;
import com.aviccxzx.smarthome.netutil.app.App;
import com.gizwits.gizwifisdk.api.GizWifiSDK;
import com.gizwits.gizwifisdk.enumration.GizWifiConfigureMode;
import com.gizwits.gizwifisdk.enumration.GizWifiErrorCode;
import com.gizwits.gizwifisdk.enumration.GizWifiGAgentType;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;


/**
 * Created by joey on 2016/10/25.
 */
public class GateWayLinkReadyAct extends GosConfigModuleBaseActivity implements View.OnClickListener {
    CheckBox cbSelect;
    /** 配置用参数 */
    String workSSID, workSSIDPsw;

    /** The String */
    String timerText;

    List<GizWifiGAgentType> modeList, modeDataList;
    /** 倒计时 */
    int secondleft = 60;
    /** The timer */
    Timer timer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setContentView(R.layout.gateway_link_ready);
        } else {
        }
        ((TextView) findViewById(R.id.title_tv)).setText("确认网关处于待连接状态");
        findViewById(R.id.ll_title_left_view).setOnClickListener(this);
        setView();
//        initData();
//        startAirlink();
    }

    private void setView() {
        cbSelect = (CheckBox) findViewById(R.id.cbSelect);
        cbSelect.setOnClickListener(this);
        findViewById(R.id.next_tv).setOnClickListener(this);
    }

    private void initData() {
        workSSID = spf.getString("workSSID", "");
        workSSIDPsw = spf.getString("workSSIDPsw", "");
        modeDataList = new ArrayList<GizWifiGAgentType>();
        modeDataList.add(GizWifiGAgentType.GizGAgentESP);
        modeDataList.add(GizWifiGAgentType.GizGAgentMXCHIP);
        modeDataList.add(GizWifiGAgentType.GizGAgentHF);
        modeDataList.add(GizWifiGAgentType.GizGAgentRTK);
        modeDataList.add(GizWifiGAgentType.GizGAgentWM);
        modeDataList.add(GizWifiGAgentType.GizGAgentQCA);
        modeDataList.add(GizWifiGAgentType.GizGAgentTI);
        modeDataList.add(GizWifiGAgentType.GizGAgentFSK);
        modeDataList.add(GizWifiGAgentType.GizGAgentMXCHIP3);
        modeDataList.add(GizWifiGAgentType.GizGAgentBL);
        modeDataList.add(GizWifiGAgentType.GizGAgentAtmelEE);
        modeDataList.add(GizWifiGAgentType.GizGAgentOther);
        modeList = new ArrayList<GizWifiGAgentType>();

        modeList.add(modeDataList
                .get(GosAirlinkChooseDeviceWorkWiFiActivity.modeNum));

    }

    private void startAirlink() {
        GizWifiSDK.sharedInstance().setDeviceOnboarding(workSSID, workSSIDPsw,
                GizWifiConfigureMode.GizWifiAirLink, null, 60, modeList);
//        handler.sendEmptyMessage(handler_key.START_TIMER.ordinal());

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_title_left_view:
                finish();
                break;
            case R.id.next_tv:
                if (cbSelect.isChecked()) {
                    Intent intent = new Intent(this, IsLinkingActivity.class);
                    startActivity(intent);
                    finish();
                }else {
                    App.getApp().showToast("请按照提示进行操作");
                }
                break;
        }
    }

    private enum handler_key {

        /**
         * 倒计时提示
         */
        TIMER_TEXT,

        /**
         * 倒计时开始
         */
        START_TIMER,

        /**
         * 配置成功
         */
        SUCCESSFUL,

        /**
         * 配置失败
         */
        FAILED,

    }

    /**
     * The handler.
     */
    Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            handler_key key = handler_key.values()[msg.what];
            switch (key) {
                case TIMER_TEXT:

//                    tvTimer.setText(timerText);
                    break;

                case START_TIMER:
//                    isStartTimer();
                    break;

                case SUCCESSFUL:
//                    App.getApp().showToast("成了");
                    GosDeviceModuleBaseActivity.deviceslist = GizWifiSDK.sharedInstance()
                            .getDeviceList();
//                    App.getApp().showToast("成了。。。"+GosDeviceModuleBaseActivity.deviceslist.toString());
                    Log.d("OK",GosDeviceModuleBaseActivity.deviceslist.toString());
                    finish();
                    break;

                case FAILED:
                    Intent intent = new Intent(GateWayLinkReadyAct.this,
                            GosDeviceReadyActivity.class);
                    startActivity(intent);
                    finish();
                    break;

                default:
                    break;
            }
        }

    };

    // 屏蔽掉返回键
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            quitAlert(this, null);
            return true;
        }
        return false;
    }

    // 倒计时
//    public void isStartTimer() {
//
//        secondleft = 60;
//        timer = new Timer();
//        timer.schedule(new TimerTask() {
//            @Override
//            public void run() {
//                secondleft--;
//                rpbConfig.setProgress((60 - secondleft) * (100 / 60.0));
//                if (secondleft == 58) {
//                    timerText = (String) getText(R.string.searching_device);
//                    handler.sendEmptyMessage(handler_key.TIMER_TEXT.ordinal());
//                } else if (secondleft == 30) {
//                    timerText = (String) getText(R.string.searched_device);
//                    handler.sendEmptyMessage(handler_key.TIMER_TEXT.ordinal());
//                } else if (secondleft == 28) {
//                    timerText = (String) getText(R.string.trying_join_device);
//                    handler.sendEmptyMessage(handler_key.TIMER_TEXT.ordinal());
//                }
//            }
//        }, 1000, 1000);
//    }

    /**
     * 设备配置回调
     *
     * @param result
     *            错误码
     * @param mac
     *            MAC
     * @param did
     *            DID
     * @param productKey
     *            PK
     */
    protected void didSetDeviceOnboarding(GizWifiErrorCode result, String mac,
                                          String did, String productKey) {
        if (GizWifiErrorCode.GIZ_SDK_DEVICE_CONFIG_IS_RUNNING == result) {
            return;
        }

        if (timer != null) {
            timer.cancel();
        }
        Message message = new Message();
        if (result == GizWifiErrorCode.GIZ_SDK_SUCCESS) {
            message.what = handler_key.SUCCESSFUL.ordinal();
        } else {
            message.what = handler_key.FAILED.ordinal();
            message.obj = toastError(result);
        }
        Log.i("Apptest", result.toString());
        handler.sendMessage(message);
    }
}
